import requests
import json


class beforeTest:

    def e_login(self):
        base_url = 'http://erp.uat1.rscloud.com/api-zendai/user/e/login'
        header = {
            'Host': 'erp.uat1.rscloud.com',
            'Proxy-Connection': 'keep-alive',
            'Content-Length': '135',
            'Accept': 'application/json, text/plain, */*',
            'Cache-Control': 'no-cache',
            'L-A-Platform': 'erp-web',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
            'currentUrl': 'http://erp.uat1.rscloud.com/#/login',
            'Content-Type': 'application/json;charset=UTF-8',
            'Origin': 'http://erp.uat1.rscloud.com',
            'Referer': 'http://erp.uat1.rscloud.com/',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9'
        }
        login_data = {
            "loginNameOrMobile":"jyx",
            "password":"OE4EhEi+CQoBYkkaya4QwQ==",
            "companyCode":"10001",
            "loginSource":"E_WEB",
            "loginType":"PASSWORD"
        }
        result = requests.post(url=base_url, headers=header, data=json.dumps(login_data), timeout=30)
        print(result.text)
        self.user_id = json.loads(result.text)['data']['id']
        get_cookie = result.headers['Set-Cookie'].split(';')
        fin_cookie = get_cookie[0]
        auth_data = fin_cookie.split('=')
        # print(auth_data[1])
        # print(fin_cookie)
        fin_header = {
            'Host': 'erp.uat1.rscloud.com',
            # 'Content-Length': '135',
            'Connection': 'keep-alive',
            'Accept': 'application/json, text/plain, */*',
            'Authorization': auth_data[1],
            'Cache-Control': 'no-cache',
            'L-A-Platform': 'erp-web',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36',
            # 'currentUrl': 'http://erp.uat1.rscloud.com/#/login',
            'Content-Type': 'application/json;charset=UTF-8',
            # 'Origin': 'http://bgerpsit.sce-re.com',
            # 'Referer': 'http://erp.uat1.rscloud.com/',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Cookie': fin_cookie,
            # 'If-Modified-Since': '0'
        }
        return fin_header

    def get_user_id(self):
        user_num = self.user_id
        # print(user_num)
        return user_num

# a = beforeTest()
# a.e_login()
# print(a.get_user_id())
